﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Mimoza.Common;

namespace Mimoza.ObjectLayer
{

    public enum ContainerType { LocalHost = -1, Managed = 0, Unmanaged = 1, Search_Folders = 2, Updates = 4,
        QueryByConnectionTime = 5, QueryByPlatform = 6 };

    public class Container : INotifyPropertyChanged
    {
        public const int Terminals = 0;
        private ContainerRecord record = new ContainerRecord();
        private OL ol;

        public Container(OL ol)
        {
            this.ol = ol;
        }
        public Container(OL ol, ContainerRecord record)
        {
            this.ol = ol;
            this.record = record;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public ContainerRecord Record
        {
            get
            {
                return record;
            }
        }
        public Guid ID
        {
            get
            {
                return record.ID;
            }
        }
        public Container Parent
        {
            get
            {
                if (record.ParentID != null)
                {
                    return ol.ContainerRepository.GetContainerByID(record.ParentID);
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (value.Record.ID != this.record.ParentID)
                {
                    record.ParentID = value.ID;
                    NotifyPropertyChanged("Parent");
                }
            }
        }
        public string Name
        {
            get { return record.Name; }
            set
            {
                if (value != this.record.Name)
                {
                    record.Name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
        public ContainerType Type
        {
            get 
            {
                return (ContainerType)record.Type;
            }
            set 
            {
                record.Type = (int)value; 
            }
        }
        public bool IsSystem
        {
            get { return record.IsSystem; }
            set { record.IsSystem = value; }
        }
        public bool IsVisible
        {
            get { return record.IsVisible; }
            set { record.IsVisible = value; }
        }
        public Guid EffectivePolicyID 
        {
            get { return record.EffectivePolicyID; }
            set
            {
                if (value != this.record.EffectivePolicyID)
                {
                    record.EffectivePolicyID = value;
                    NotifyPropertyChanged("EffectivePolicyID");
                }
            }
        }
        public DateTime UpdateDate
        {
            get
            {
                return record.UpdateDate;
            }
        }

        public IEnumerable<Container> GetChilds()
        {
            return ol.ContainerRepository.GetChilds(this);
        }
        public IEnumerable<Policy> GetEffectivePolicy()
        {
            return ol.ContainerRepository.GetEffectivePolicy(this);
        }
        public IEnumerable<Policy> GetDirectPolicy()
        {
            return ol.ContainerRepository.GetDirectPolicy(this);
        }
        public IEnumerable<Policy> GetInDirectPolicy()
        {
            return ol.ContainerRepository.GetInDirectPolicy(this);
        }
        public IEnumerable<Host> GetHosts()
        {
            return ol.ContainerRepository.GetHosts(this);
        }
    }
}
